package com.zy.test.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zy.test.entity.Student;
import com.zy.test.service.StudentService;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;

/**
 * 学生表
 * 学生ID，学号，姓名，院系，专业，班级ID，年龄，性别，联系方式，地址，备注。学生ID为主键，班级I(Student)表控制层
 *
 * @author jhr
 * @since 2023-04-03 16:10:43
 */
@RestController
@RequestMapping("student")
public class StudentController extends ApiController {
    /**
     * 服务对象
     */
    @Resource
    private StudentService studentService;

    /**
     * 分页查询所有数据
     *
     * @param student 查询实体
     * @return 所有数据
     */
    @GetMapping("/selectAllPaging")
    public R selectAllPaging(Student student) {
        Page<Student> userPage = new Page<>(student.getPageNum(), student.getPageSize());
        LambdaQueryWrapper<Student> queryWrapper = new LambdaQueryWrapper<>();
        if (!ObjectUtils.isEmpty(student.getStCode())){
            queryWrapper.eq(Student::getStCode,student.getStCode());
        }
        if (!ObjectUtils.isEmpty(student.getStName())){
            queryWrapper.like(Student::getStName,student.getStName());
        }
        if (!ObjectUtils.isEmpty(student.getClCode())){
            queryWrapper.like(Student::getClCode,student.getClCode());
        }
        return success(this.studentService.page(userPage,queryWrapper));
    }

    @GetMapping("/selectAll")
    public R selectAll() {
        return success(this.studentService.list());
    }
    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("/selectById/{id}")
    public R selectOne(@PathVariable Serializable id) {
        return success(this.studentService.getById(id));
    }

    /**
     * 新增数据
     *
     * @param student 实体对象
     * @return 新增结果
     */
    @PostMapping("/addStudent")
    public R insert(@RequestBody Student student) {
        return success(this.studentService.save(student));
    }

    /**
     * 修改数据
     *
     * @param student 实体对象
     * @return 修改结果
     */
    @PutMapping("/updateStudent")
    public R update(@RequestBody Student student) {
        return success(this.studentService.updateById(student));
    }

    /**
     * 删除数据
     *
     * @param idList 主键结合
     * @return 删除结果
     */
    @DeleteMapping("/deleteByIds")
    public R delete(@RequestParam("idList") List<Long> idList) {
        return success(this.studentService.removeByIds(idList));
    }
}

